<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width,initial-scale=1.0" />
		<link rel="shortcut icon" href="favicon.ico" />
		<link rel="bookmark" href="favicon.ico" type="image/x-icon"　/>
		<title></title>
		<link rel="stylesheet" href="css/reset.css" />
		<link rel="stylesheet" href="css/main.css" />
		<style>
			.m-artical-action-mid{
				position: fixed;
				width: 100%;
				height: 40%;
				top: 30%;
				z-index: 9998;
			}
		</style>
	</head>
	<body>
		<!--中间点击层-->
		<div class="m-artical-action">
			<div class="m-artical-action-mid" id="action_mid"></div>
		</div>
		<!--顶部导航-->
		<div class="nav-top" id="nav_top" style="display: none;">
			<div class="nav_container">
				<div class="nav_return"></div>
				<div class="nav_text">返回书架</div>
			</div>
		</div>
		<!--主体内容-->
		<div class="container">
			<div class="Content">
				
				
			</div>
			<div class="page_btn">
					<button class="btn-prev" id="btn_prev">上一页</button>
					<button class="btn-next" id="btn_next">下一页</button>
				</div>
		</div>

		<!--底部导航栏    -->
		<div class="nav-bottom" id="nav_bottom" style="display: none;">
			<div class="btn-mulu">
				<div class="mulu">
					<div class="icon_mulu"> </div>
					<div class="text_mulu">目录</div>
				</div>
			</div>
			<div class="btn_Aa">
				<div class="Aa">
					<div class="icon-Aa" id="icon_Aa"> </div>
				<div class="text_Aa">字体</div>
				</div>
			</div>
			<div class="btn-yejian">
				<div class="yejian">
					<div class="icon-yejian" id="icon_yejian"> </div>
					<div class="text_yejian">夜间</div>
				</div>
			</div>
			
			<!--字体功能栏-->
		<div class="fontPop" id="font_Pop" style="display: none;">
			<div class="fontSize">
				<span>字号</span>
				<button class="btnBig" id="btn_Big">大</button>
				<button class="btnSmall" id="btn_Small">小</button>
			</div>
			<div class="fontBk">
				<span>背景</span>
				<div class="bkColor">
					<div class="bk-container" style="background-color: #f7eee5;">
						<div id="mb"><span >	米白	</span></div>
					</div>
					<div class="bk-container" style="background-color: #e9dfc7;">
						<div id="zz"><span >	纸张	</span></div>
					</div>
					<div class="bk-container" style="background-color: #a4a4a4;">
						<div id="qh"><span >	浅灰	</span></div>
					</div>
					<div class="bk-container" style="background-color: #cdefce;">
						<div id="hy"><span >	护眼	</span></div>
					</div>
					<div class="bk-container" style="background-color: #283548;">
						<div id="hl"><span >	海蓝	</span></div>
					</div>
				</div>
			</div>
		</div>
		</div>
		
		<script type="text/javascript" src="js/jquery.min.js" ></script>
		<script type="text/javascript" src="js/jquery.jsonp.js" ></script>
		<script type="text/javascript" src="js/jquery.base64.js" ></script>
		<script>
		var Dom={
					font_Pop: $("#font_Pop"),
					nav_top:$("#nav_top"),
					nav_bottom:$("#nav_bottom"),
					icon_Aa:$("#icon_Aa"),
					body:$("body"),
					win:$(window),
					icon_yejian:$("#icon_yejian"),
					btn_Big:$("#btn_Big"),
					btn_Small:$("#btn_Small")
			}
		
		//数据层的初始化
		var RootContainer = $('.Content');
		var readerUIFrame = ReaderBaseFrame(RootContainer);
		
		var readerm=ReaderModel();
		readerm.init(function(data){
			readerUIFrame(data);
		});
		//业务事件的初始化
		EventHandler();
		
		
		var Util=(function(){//封装的本地存储和json数据解析
				var prefix = 'function_reader_';
				var StorageGetter = function(key){
					return localStorage.getItem(prefix + key);
				}
				var StorageSetter = function(key,val){
					return localStorage.setItem(prefix+key,val);
				}
				//通过url来获取加密信息，并解密
				var getJsonp=function(url,callback){
					return $.jsonp({
						url:url,
						cache:true,
						callback:'duokan_fiction_chapter',
						success:function(result){
							//获取加密信息，解码、转json
							var data=$.base64.decode(result);
							var json=decodeURIComponent(escape(data));
							callback(json);
						}
					})
				}
				return{
					getJsonp:getJsonp,
					StorageGetter:StorageGetter,
					StorageSetter:StorageSetter
				}
			})();
			
		//背景颜色 存储记录
		var bkgroundcolor=Util.StorageGetter("bkgroundcolor");
		var bkCurrColor=Util.StorageGetter("bkgroundcolor");
		Dom.body.css('background-color',bkgroundcolor);
		//字号大小 设置存储记录
		var InitFontSize;
		InitFontSize=Util.StorageGetter("font_size");
		InitFontSize= parseInt(InitFontSize);
		if (!InitFontSize) {
				InitFontSize = 18;
			}
		$("p").css('font-size',InitFontSize);
		
			
		function ReaderBaseFrame(container){//UI渲染层
			
			function ParseChapterData(jsonData){//生成要展示的 html文本
				var jsonObj=JSON.parse(jsonData);
				var html="<h4>" + jsonObj.t + "</h4>";
				for (var i=0; i<jsonObj.p.length; i++) {
					html+="<p>"+jsonObj.p[i]+"</p>";
				}
				return html;
			}
			
			return function (data){//返回 渲染后的结果data：解密后的json数据
				container.html(ParseChapterData(data));
			}
		}
		
		function ReaderModel(){//数据交互层
			//todo 获取章节信息
			var Chapter_id;
			var ChapterTotal;
			var init=function(Uicallback){//初始化 信息
				getFictionInfo(function(){
					getCurChaptInfo(Chapter_id,function(data){
						//todo 
						Uicallback&&Uicallback(data);
					})
				})
			};
			
			var getFictionInfo=function(callback){//获取所有章节摘要信息
				$.get('data/chapter.json',function(data){
					//todo 获取章节信息之后的回调
					Chapter_id = parseInt(Util.StorageGetter("ChapterId"));
					if (Chapter_id == null|| !Chapter_id) {
						Chapter_id=data.chapters[1].chapter_id;
					}
					//Chapter_id=data.chapters[1].chapter_id;
					ChapterTotal=data.chapters.length;
					callback&&callback();
				},'json');
			};
			
			var getCurChaptInfo=function(Chapter_id,callback){//获取当前Chapter_id章节详细信息
				$.get('data/data'+Chapter_id+'.json',function(data){
					//todo 获取详细信息之后的回调
					if(data.result == 0){
						var url=data.jsonp;//jsonp URL
						Util.getJsonp(url,function(data){
							callback && callback(data);
						});//通过url来获取加密信息，并解密
					}
				},'json');
			};
			
			var prveChapter=function(){//上一页				
				Chapter_id=parseInt(Chapter_id);
				if(Chapter_id == 0){
					return;
				}
				Chapter_id -= 1;
				//调用方法获取 Chapter_id章节信息
				getCurChaptInfo(Chapter_id,function(data){
					//console.log(data);
					readerUIFrame(data);//渲染html数据
				});
				Util.StorageSetter("ChapterId",Chapter_id);
			};
			var nextChapter=function(){//下一页
				
				Chapter_id=parseInt(Chapter_id);
				if(Chapter_id == ChapterTotal){
					return;
				}
				Chapter_id += 1;
				//调用方法获取 Chapter_id章节信息
				getCurChaptInfo(Chapter_id,function(data){
					//console.log(data);
					readerUIFrame(data);
				});
				Util.StorageSetter("ChapterId",Chapter_id);
			};
			
			return{
				init:init,
				prveChapter:prveChapter,
				nextChapter:nextChapter
			}
		}
		
		function EventHandler(){//业务事件处理层
			//点击主体中间事件
			$("#action_mid").click(function(){
				if(Dom.nav_top.css('display')=="none"){
					Dom.nav_top.show();
					Dom.nav_bottom.show();
				}else{
					Dom.nav_top.hide();
					Dom.nav_bottom.hide();
					Dom.font_Pop.hide();
					Dom.icon_Aa.css('border','');
					Dom.icon_yejian.css('border','');
				}
			});
			//点击字体事件
			$(".btn_Aa").click(function(){
				if (Dom.font_Pop.css('display') == 'none') {
					Dom.icon_Aa.css('border','1px solid #FF7800');
					Dom.font_Pop.show();
				} else{
					Dom.icon_Aa.css('border','');
					Dom.font_Pop.hide();
				}
			});
			//点击夜间事件
			$(".btn-yejian").click(function(){
				if (Dom.body.css('background-color')!="rgb(40, 53, 72)") {
					bkCurrColor='#283548';
					//$(".bkColor").find('.bk-container-current')[0].className='';
					Dom.body.css('background-color',bkCurrColor);
					Dom.icon_yejian.css('border','1px #FF7800 solid');
					$("#hl").addClass('bk-container-current');
					
				} else{
					bkCurrColor='#e9dfc7';
					Dom.body.css('background-color',bkCurrColor);
					Dom.icon_yejian.css('border','');
					$("#hl").removeClass('bk-container-current');
					$("#mb").addClass('bk-container-current');
				}
				Util.StorageSetter("bkgroundcolor",bkCurrColor);
			});
			//滚动条事件
			Dom.win.scroll(function(){
				Dom.nav_top.hide();
				Dom.nav_bottom.hide();
				Dom.font_Pop.hide();
				Dom.icon_Aa.css('border','');
				Dom.icon_yejian.css('border','');
			})
			//点击 大、小事件
			Dom.btn_Big.click(function(){
				if(InitFontSize>=20){
					return;
				}
				InitFontSize+=1;
				console.log(InitFontSize);
				$("p").css('font-size',InitFontSize);
				Util.StorageSetter("font_size",InitFontSize);
			});
			Dom.btn_Small.click(function(){
				if(InitFontSize<=12){
					return;
				}
				InitFontSize-=1;
				console.log(InitFontSize);
				$("p").css('font-size',InitFontSize);
				Util.StorageSetter("font_size",InitFontSize);
			});
			//点击背景颜色切换
			$(".bk-container").bind('click',function(){
				if ($(".bkColor").find('.bk-container-current').length>0) {
					$(".bkColor").find('.bk-container-current')[0].className='';
				} else{
					
				}
				var id= this.firstElementChild.id;
				$("#"+id).addClass('bk-container-current');
				
				switch (id){
					case 'mb':
					Dom.body.css('background-color','#f7eee5');
						break;
						case 'zz':
					Dom.body.css('background-color','#e9dfc7');
						break;
						case 'qh':
					Dom.body.css('background-color','#a4a4a4');
						break;
						case 'hy':
					Dom.body.css('background-color','#cdefce');
						break;
						case 'hl':
					Dom.body.css('background-color','#283548');
						break;
					default:
						break;
				}
			Util.StorageSetter("bkgroundcolor",Dom.body.css('background-color'));
			});
			
			$("#btn_prev").click(function(){
				readerm.prveChapter();
			});
			
			$("#btn_next").click(function(){
				readerm.nextChapter();
			});
		}
		</script>
	</body>

</html>